﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Proggy.Data.Concrete;
using Proggy.Data.Entities;
using Proggy.Web.Cms.Membership;
using Newtonsoft.Json;
using Proggy.Web.Cms.ExampleDomain.ContentModels;

namespace Proggy.Web.Cms.ExampleDomain.Seeding
{
    public class ExampleSeed : DropCreateDatabaseIfModelChanges<ProggyDataContext>
    {
        protected override void Seed(ProggyDataContext context)
        {
            try
            {

                /*
                 * This is an example of how to seed the database.
                 * 
                 * This is important when devving or when deploying your site for the first time 
                 * 
                 * Some things ARE MANDATORY THOUGH - please include mandatory examples when creating 
                 * your seed file
                 * 
                 */




                /*
                 * First create the MANDATORY user groups
                 */

                var backenders = new Group { GroupName = MembershipConstants.BACK_END_USER_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(backenders);
                var editors = new Group { GroupName = MembershipConstants.EDITOR_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(editors);
                var globals = new Group { GroupName = MembershipConstants.GLOBAL_ADMIN_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(globals);
                var membmanagers = new Group { GroupName = MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(membmanagers);
                var publishers = new Group { GroupName = MembershipConstants.PUBLISHER_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(publishers);
                var randomdatas = new Group { GroupName = MembershipConstants.RANDOM_DATA_EDITOR_GROUP_NAME, FirstCreated = DateTime.Now, FirstCreatedUsername = "jbloggs", LastUpdated = DateTime.Now, LastUpdatedUsername = "jbloggs" };
                context.Groups.Add(randomdatas);







                /*
                 * Now add a global admin (mandatory) and a couple of random users
                 * 
                 * this is assuming you are using forms auth which you will be as Windows Auth is not complete
                 * in this release!
                 * 
                 * If for whatever reason you are using windows auth, just create the user-group associations
                 * 
                 */

                // create new forms auth person
                var globalAdmin = new FormsAuthenticationMember
                {
                    Created = DateTime.Now,
                    Email = "test@test.com",
                    EnteredPassword = "admin123",
                    IsActive = true,
                    Name = "Joe Bloggs",
                    Username = "jbloggs"
                };

                // hash the password
                globalAdmin.SetHash();

                // add
                context.FormsAuthenticationMembers.Add(globalAdmin);

                // add to group
                globals.GroupMembers.Add(new GroupMember { Username = globalAdmin.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });



                // create random user
                var randomUser = new FormsAuthenticationMember
                {
                    Created = DateTime.Now,
                    Email = "test2@test.com",
                    EnteredPassword = "user123",
                    IsActive = true,
                    Name = "Sarah Smith",
                    Username = "ssmith"
                };

                // hash the password
                randomUser.SetHash();

                // add
                context.FormsAuthenticationMembers.Add(randomUser);

                // add to groups
                backenders.GroupMembers.Add(new GroupMember { Username = randomUser.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });
                editors.GroupMembers.Add(new GroupMember { Username = randomUser.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });
                randomdatas.GroupMembers.Add(new GroupMember { Username = randomUser.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });




                // create random user 2
                var randomUser2 = new FormsAuthenticationMember
                {
                    Created = DateTime.Now,
                    Email = "test3@test.com",
                    EnteredPassword = "user123",
                    IsActive = true,
                    Name = "Donald Duck",
                    Username = "dduck"
                };

                // hash the password
                randomUser2.SetHash();

                // add
                context.FormsAuthenticationMembers.Add(randomUser2);

                // add to group
                backenders.GroupMembers.Add(new GroupMember { Username = randomUser2.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });
                membmanagers.GroupMembers.Add(new GroupMember { Username = randomUser2.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });
                publishers.GroupMembers.Add(new GroupMember { Username = randomUser2.Username, Created = DateTime.Now, CreatedUsername = "jbloggs" });



                /*
                 * YOU COULD NOW ADD SOME SEED DATA (ie a home page, a file store etc, but this is
                 * up to you - this can be done in the GUI just as easily, although when devving, it may be
                 * easier to always have this data available from here?)
                 */


                //doLoadTestSeed(context, 5000);
                //doSortAndMoveTestSeed(context);


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///// <summary>
        ///// Adds a bit of tree structure!!
        ///// </summary>
        ///// <param name="context"></param>
        //private void doSortAndMoveTestSeed(ProggyDataContext context)
        //{
        //    // create home
        //    var home = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.ExampleDomain.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.ExampleDomain.Controllers",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 0,
        //        OrderInParent = 0,
        //        SavedContentModelData = JsonConvert.SerializeObject(new RootContentModel
        //        {
        //            SeoDescription = new Core.PropertyTypes.CharLimitedTextString
        //            {

        //                SavedValue = Guid.NewGuid().ToString() + Guid.NewGuid().ToString()
        //            }
        //        }),
        //        Name = "Home",
        //        Template = "Root",
        //        TypeAlias = "Root",
        //        TypeAssemblyName = "Proggy.Web.Cms.ExampleDomain",
        //        UrlPortion = ""
        //    };
        //    context.PublishableNodes.Add(home);

        //    // add three at level 1
        //    var home1 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 1,
        //        OrderInParent = 0,
        //        SavedContentModelData = null,
        //        Name = "H.1",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h1",
        //        ParentNode = home
        //    };
        //    context.PublishableNodes.Add(home1);
        //    var home2 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 1,
        //        OrderInParent = 1,
        //        SavedContentModelData = null,
        //        Name = "H.2",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h2",
        //        ParentNode = home
        //    };
        //    context.PublishableNodes.Add(home2);
        //    var home3 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 1,
        //        OrderInParent = 2,
        //        SavedContentModelData = null,
        //        Name = "H.3",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h3",
        //        ParentNode = home
        //    };
        //    context.PublishableNodes.Add(home3);

        //    // add three under h1
        //    var home1_1 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 2,
        //        OrderInParent = 0,
        //        SavedContentModelData = null,
        //        Name = "H.1.1",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h11",
        //        ParentNode = home1
        //    };
        //    context.PublishableNodes.Add(home1_1);
        //    var home1_2 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 2,
        //        OrderInParent = 1,
        //        SavedContentModelData = null,
        //        Name = "H.1.2",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h12",
        //        ParentNode = home1
        //    };
        //    context.PublishableNodes.Add(home1_2);
        //    var home1_3 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 2,
        //        OrderInParent = 2,
        //        SavedContentModelData = null,
        //        Name = "H.1.3",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h113",
        //        ParentNode = home1
        //    };
        //    context.PublishableNodes.Add(home1_3);

        //    // add three under h.1.2
        //    var home1_2_1 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 3,
        //        OrderInParent = 0,
        //        SavedContentModelData = null,
        //        Name = "H.1.2.1",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h121",
        //        ParentNode = home1_2
        //    };
        //    context.PublishableNodes.Add(home1_2_1);
        //    var home1_2_2 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 3,
        //        OrderInParent = 1,
        //        SavedContentModelData = null,
        //        Name = "H.1.2.2",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h122",
        //        ParentNode = home1_2
        //    };
        //    context.PublishableNodes.Add(home1_2_2);
        //    var home1_2_3 = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.Core.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.Core.Controllers.ContentModel",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 3,
        //        OrderInParent = 2,
        //        SavedContentModelData = null,
        //        Name = "H.1.2.3",
        //        Template = "",
        //        TypeAlias = "ProggyFileFolder",
        //        TypeAssemblyName = "Proggy.Web.Cms.Core",
        //        UrlPortion = "h123",
        //        ParentNode = home1_2
        //    };
        //    context.PublishableNodes.Add(home1_2_3);

        //}

        ///// <summary>
        ///// Loads n nodes in
        ///// </summary>
        ///// <param name="context"></param>
        //private void doLoadTestSeed(ProggyDataContext context, int load)
        //{
        //    // create home
        //    var home = new PublishableNode
        //    {
        //        ContentModelNameSpace = "Proggy.Web.Cms.ExampleDomain.ContentModels",
        //        ControllerNameSpace = "Proggy.Web.Cms.ExampleDomain.Controllers",
        //        FirstCreated = DateTime.Now,
        //        FirstCreatedUsername = "jbloggs",
        //        FirstPublished = DateTime.Now,
        //        LastPublished = DateTime.Now,
        //        LastPublishedUsername = "jbloggs",
        //        LastUpdated = DateTime.Now,
        //        LastUpdatedUsername = "jbloggs",
        //        LevelInTree = 0,
        //        OrderInParent = 0,
        //        SavedContentModelData = JsonConvert.SerializeObject(new RootContentModel
        //        {
        //            SeoDescription = new Core.PropertyTypes.CharLimitedTextString
        //            {

        //                SavedValue = Guid.NewGuid().ToString() + Guid.NewGuid().ToString()
        //            }
        //        }),
        //        Name = "Home",
        //        Template = "Root",
        //        TypeAlias = "Root",
        //        TypeAssemblyName = "Proggy.Web.Cms.ExampleDomain",
        //        UrlPortion = ""
        //    };
        //    context.PublishableNodes.Add(home);

        //    context.SaveChanges();


        //    for (int i = 0; i < load; i++)
        //    {
        //        //
        //        var newNode = new PublishableNode
        //        {
        //            ContentModelNameSpace = "Proggy.Web.Cms.ExampleDomain.ContentModels",
        //            ControllerNameSpace = "Proggy.Web.Cms.ExampleDomain.Controllers",
        //            FirstCreated = DateTime.Now,
        //            FirstCreatedUsername = "jbloggs",
        //            FirstPublished = DateTime.Now,
        //            LastPublished = DateTime.Now,
        //            LastPublishedUsername = "jbloggs",
        //            LastUpdated = DateTime.Now,
        //            LastUpdatedUsername = "jbloggs",
        //            //LevelInTree = 1,
        //            //OrderInParent = i,
        //            SavedContentModelData = JsonConvert.SerializeObject(new StandardContentModel
        //            {
        //                SeoDescription = new Core.PropertyTypes.CharLimitedTextString
        //                {
        //                    SavedValue = Guid.NewGuid().ToString() + Guid.NewGuid().ToString()
        //                },
        //                MainContent = new Core.PropertyTypes.RichTextContent
        //                {
        //                    SavedValue = Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString()
        //                }
        //            }),
        //            Name = Guid.NewGuid().ToString(),
        //            Template = "Standard",
        //            TypeAlias = "Standard",
        //            TypeAssemblyName = "Proggy.Web.Cms.ExampleDomain",
        //            UrlPortion = Guid.NewGuid().ToString().Replace("_", "-")
        //        };

        //        var rnd = new Random().Next(1, context.PublishableNodes.Local.Count);

        //        var parentNode = context.PublishableNodes.Local.SingleOrDefault(x => x.PublishableNodeId == rnd);
        //        newNode.LevelInTree = parentNode.LevelInTree + 1;
        //        newNode.OrderInParent = parentNode.ChildNodes.Count;

        //        parentNode.ChildNodes.Add(newNode);
        //        context.SaveChanges();
        //    }
        //}
    }

}
